# ==UserScript==
# @name         飞书备份
# @description  登录后自动备份飞书文件
# @version      0.0.0
# @grant        GM_xmlhttpRequest
# @grant        GM.cookie
# @namespace    https://wmdyy.coding.net/p/dyy/d/tampermonkey
# @author       xpure@foxmail.com
# @updateURL    https://gitee.com/xvcer/feishu-backup/raw/master/version
# @downloadURL  https://gitee.com/xvcer/feishu-backup/raw/master/lib/main.js
# @require      https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/axios/0.21.1/axios.min.js
# @include      https://xvccapical.feishu.cn/*
# ==/UserScript==


API = "https://1115475855755722.cn-qingdao.fc.aliyuncs.com/2016-08-15/proxy/feishu-backup.LATEST/backup/"
#API = 'http://127.0.0.1:9000/'

get = (url)=>
  {data} = await axios.get url
  data

do =>
  bktime = parseInt(new Date()/3600000).toString 36
  if bktime == localStorage.bktime
    return

  cookie = (
    name+"="+value for {name,value} from await GM.cookie.list({})
  ).join '; '


  {code, data} = await get 'https://xvccapical.feishu.cn/space/api/user/'
  if code == 0
    await axios.post(
      API+'cookie'
      [
        data.id
        cookie
      ]
    )

  localStorage.bktime = bktime


